package com.vf.cloud.paas.app.master.api.render.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.vf.cloud.paas.app.master.common.domain.RenderLog;
import com.vf.cloud.paas.common.security.annotation.TokenRequired;
import com.vf.cloud.paas.common.util.R;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/render/log")
public class RenderLogController {
	@TokenRequired
	@GetMapping(value = { "/pages" }, produces = "application/json; charset=utf-8")
	public R<Page<RenderLog>> pages(HttpServletRequest request,
			@RequestParam(name = "startTime", required = false, defaultValue = "") String startTime,
			@RequestParam(name = "endTime", required = false, defaultValue = "") String endTime,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {

		String sqlExceptSelect = " FROM " + RenderLog.TABLE_NAME + " a  WHERE 1=1 ";
		if (!StrKit.isBlank(startTime) && !StrKit.isBlank(endTime)) {
			sqlExceptSelect += " AND ( a.ONLINE_TIME between '"+startTime+"' and '"+endTime+"'  ) ";
		}
		sqlExceptSelect += "  ORDER  BY   a.ONLINE_TIME desc ";
		
		Page<RenderLog> pages = RenderLog.dao.paginate(cur, limit, "select a.*,(select name from master_scene where id=a.scene_id  ) as scenename ,"
				+ " (select name from master_user where id=a.create_user_id  ) as username,"
				+ " (select name from master_render_agent where id=a.agent_id  ) as agentname,"
				+ " (select ip from master_render_agent where id=a.agent_id  ) as agentip,"
				+ " (select name from master_render_agent_gpu where id=a.agent_gpu_id  ) as gpuname,"
				+ " (select g.`index` from master_render_agent_gpu g where g.id=a.agent_gpu_id  ) as gpuindex  ", sqlExceptSelect);
		return R.ok(pages);
	}
}
